package com.dmx.fw.web.struts2.components;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.components.Radio;
import org.apache.struts2.util.MakeIterator;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import com.dmx.fw.bean.UIComponentBean;
import com.dmx.fw.web.util.Helper;
import com.opensymphony.xwork2.util.ValueStack;

@StrutsTag(
	    name="radio",
	    tldTagClass="com.dmx.fw.web.struts2.views.tags.RadioTag",
	    description="Render a select element",
	    allowDynamicAttributes=true)
public class ExtRadio extends Radio {

	protected String pageState;
	protected String addState;
	protected String editState;
	protected String viewState;
	protected String code;
	protected String params;
	protected String outHidden;
	protected Object exclude;
	
	public ExtRadio(ValueStack stack, HttpServletRequest request,
			HttpServletResponse response) {
		super(stack, request, response);
	}
	
	protected String getDefaultTemplate() {
        return "extradio";
    }
	
	public void evaluateExtraParams() {
		if(list == null || list.equals("")){
			list = "1";
		}
		super.evaluateExtraParams();
		
		if (value != null) {
	        addParameter("value", findString(value));
	     }
		
		if (pageState != null) {
	        addParameter("pageState", findString(pageState));
	     }
		 if (addState != null) {
		        addParameter("addState", findString(addState));
		     }
		 if (editState != null) {
		        addParameter("editState", findString(editState));
		     }
		 if (viewState != null) {
		        addParameter("viewState", findString(viewState));
		     }
		 if (code != null) {
			 	List<UIComponentBean> retList;
		        //addParameter("code", findString(code));
		        if(listKey == null || listKey.equals("")){
		        	addParameter("listKey","key");
		        }
		        if(listValue == null || listValue.equals("")){
		        	addParameter("listValue","value");
		        }
		        if (params != null) {
		        	String[] paramsList =StringUtils.split(findString(params), ",");
		        	retList = Helper.getCodeListToUIComponentBean(findString(code), Arrays.asList(paramsList));
			     }else{
			    	retList = Helper.getCodeListToUIComponentBean(findString(code));
			     }
		        if(retList != null && retList.size() > 0){
		        	 if (exclude != null) {
		        		 List excludeValue = null;
		             	
		             	 if (exclude instanceof String) {
		                  	Object tmp = findValue((String) exclude);
		                  	if(tmp instanceof List){
		                  		excludeValue = (List) tmp;
		                  	}else{
		                  		excludeValue = new ArrayList();
		                  		excludeValue.add(tmp);
		                  	}
		                  	
		                  } else if (exclude instanceof List) {
		                  	excludeValue = (List) exclude;
		                  
		                  }
		             	 for(int i=0;i<excludeValue.size();i++){
		             		 for(int j=0;j<retList.size();j++){
		             			if(retList.get(j).getKey().equals(excludeValue.get(i).toString())){
		             				retList.remove(retList.get(j));
		             				break;
		             			}
		             		 }
		             	 }
		             	
		        	 }
		        	addParameter("list",MakeIterator.convert(retList));
		        	addParameter("listSize",retList.size());
		        }
		        
		     }
		 
		 if (outHidden != null) {
		        addParameter("outHidden", findString(outHidden));
		     }
		 
       
	}
	@StrutsTagAttribute(description="pageState attribute", type="String")
	public void setPageState(String pageState) {
		this.pageState = pageState;
	}

	@StrutsTagAttribute(description="addState attribute", type="String")
	public void setAddState(String addState) {
		this.addState = addState;
	}

	@StrutsTagAttribute(description="editState attribute", type="String")
	public void setEditState(String editState) {
		this.editState = editState;
	}

	@StrutsTagAttribute(description="viewState attribute", type="String")
	public void setViewState(String viewState) {
		this.viewState = viewState;
	}

	@StrutsTagAttribute(description="code attribute", type="String")
	public void setCode(String code) {
		this.code = code;
	}
	
	@StrutsTagAttribute(description="params attribute", type="String")
	public void setParams(String params) {
		this.params = params;
	}
	
	@StrutsTagAttribute(description="outHidden attribute", type="Boolean")
	public void setOutHidden(String outHidden) {
		this.outHidden = outHidden;
	}
	
	@StrutsTagAttribute(description="exclude attribute")
	public void setExclude(Object exclude) {
		this.exclude = exclude;
	}
	
}
